/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

/**
 * This class translate the complete play element
 *
 */
public class CompletePlayTranslator {

    /**
     * Creates a new instance of CompletePlayTranslator    
     * @param node The complete play node of the Dom structure
     * @param ont The ontology
     * @param instPlay The play instance
     * @param nsName The namespace
     */
    public CompletePlayTranslator(Node node, OntModel ont, Individual instPlay,
            String nsName) {

        lastInstanceSLDTranslator lastInst =
                new lastInstanceSLDTranslator();


        try {

            OntClass completePlay =
                    ont.getOntClass(nsName + "Complete-Play");
            OntClass act =
                    ont.getOntClass(nsName + "Act");

            DatatypeProperty identifier =
                    ont.getDatatypeProperty(nsName + "identifier");
            DatatypeProperty timeLimit =
                    ont.getDatatypeProperty(nsName + "time-limit");
            ObjectProperty whenLastActCompleted =
                    ont.getObjectProperty(nsName + "when-last-act-completed");

            String instance =
                    "complete_play_instance_" +
                    (String.valueOf(lastInst.lastInstance(completePlay)));
            NodeList nodeChildren =
                    node.getChildNodes();
            if (nodeChildren != null) {
                Individual instComPlay =
                        ont.createIndividual(nsName + instance, completePlay);

                ObjectProperty completePlayRef =
                        ont.getObjectProperty(nsName + "complete-play-ref");
                instPlay.setPropertyValue(completePlayRef, instComPlay);

                for (int l =
                        0; l < nodeChildren.getLength(); l++) {
                    if (nodeChildren.item(l).getNodeName().compareTo("imsld:when-last-act-completed") ==
                            0) {
                        NamedNodeMap attrsItem =
                                nodeChildren.item(l).getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("ref") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode identifierValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                searchInstance searchInst =
                                        new searchInstance();
                                Individual instAct =
                                        searchInst.search(act, identifier,
                                        identifierValue);
                                instComPlay.addProperty(whenLastActCompleted,
                                        instAct);
                            }

                        }
                    }
                    if (nodeChildren.item(l).getNodeName().compareTo("imsld:time-limit") ==
                            0) {
                        NamedNodeMap attrsItem =
                                nodeChildren.item(l).getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("property-ref") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#duration");
                                RDFNode timeLimitValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instComPlay.setPropertyValue(timeLimit,
                                        timeLimitValue);
                            }

                        }
                    }
                }//for
            }//if                     
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
